package net.phoenix.repository.value;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;

import net.phoenix.repository.RepositoryRuntimeException;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class BooleanValue extends BasicValue {
	private boolean value;

	public BooleanValue(boolean value) {
		this.value = value;
	}
	public BooleanValue(String value) {
		this.value = Boolean.getBoolean(value);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see net.phoenix.repository.value.BasicValue#getType()
	 */
	@Override
	public int getType() {
		return PropertyType.BOOLEAN;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.phoenix.repository.value.BasicValue#getString()
	 */
	@Override
	public String getString() {
		return Boolean.toString(this.value);
	}



	@Override
	public long getLong() {
		if (this.value)
			return 1;
		else
			return 0;
	}

	@Override
	public double getDouble() {
		if (this.value)
			return 1;
		else
			return 0;
	}

	@Override
	public boolean getBoolean() {
		return this.value;
	}

	@Override
	public int compareTo(Value o) {
		Value another = (Value) o;
		try {
			return Boolean.valueOf(this.value).compareTo(Boolean.valueOf(another.getBoolean()));
		} catch (RepositoryException e) {
			throw new RepositoryRuntimeException(e);
		}
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof Value) {
			if (this.compareTo((Value)o) == 0)
				return true;
		}
		return false;
	}

	
}
